package cn.juque.jdfsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.jdfsystem.entity.PlatformInfo;
import cn.juque.jdfsystem.service.IPlatformInfoService;
import cn.juque.jdfsystem.vo.platforminfo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>PlatformInfoController</li>
 * </ul>
 * @date 2023-01-13 15:23:59
 **/
@Slf4j
@Controller
@RequestMapping("/jdf/platformInfo")
public class PlatformInfoController extends BaseController {

    @Resource
    private IPlatformInfoService platformInfoService;

    /**
     * 查询
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public BaseResponseVO<List<PlatformInfoListVO>> list(@RequestBody BaseRequestVO<PlatformInfoSearchVO> request) {
        PlatformInfoSearchVO searchVO = super.validatorUtil.checkNotNull(request);
        PageInfo pageInfo = request.getPageInfo();
        super.validatorUtil.checkPageInfo(pageInfo);
        PlatformInfo platformInfo = new PlatformInfo();
        BeanUtil.copyProperties(searchVO, platformInfo);
        List<PlatformInfo> list = this.platformInfoService.listInfoWithPage(pageInfo, platformInfo);
        List<PlatformInfoListVO> voList = BeanUtil.copyToList(list, PlatformInfoListVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public BaseResponseVO<PlatformInfoDetailVO> detail(String id) {
        Assert.notEmpty(id);
        PlatformInfo platformInfo = this.platformInfoService.detail(id);
        PlatformInfoDetailVO detailVO = new PlatformInfoDetailVO();
        BeanUtil.copyProperties(platformInfo, detailVO);
        return new BaseResponseVO<>(MessageEnum.OK, detailVO);
    }

    /**
     * 新增
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public BaseResponseVO<Boolean> add(@RequestBody BaseRequestVO<PlatformInfoAddVO> request) {
        super.validatorUtil.fastValidate(request);
        PlatformInfoAddVO addVO = request.getParam();
        PlatformInfo platformInfo = new PlatformInfo();
        BeanUtil.copyProperties(addVO, platformInfo);
        this.platformInfoService.saveInfo(platformInfo);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 更新
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public BaseResponseVO<Boolean> edit(@RequestBody BaseRequestVO<PlatformInfoUpdateVO> request) {
        super.validatorUtil.fastValidate(request);
        PlatformInfoUpdateVO updateVO = request.getParam();
        PlatformInfo platformInfo = new PlatformInfo();
        BeanUtil.copyProperties(updateVO, platformInfo);
        this.platformInfoService.updateInfo(platformInfo);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 删除
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> delete(@RequestBody BaseRequestVO<PlatformInfoDeleteVO> request) {
        super.validatorUtil.fastValidate(request);
        List<String> idList = request.getParam().getIdList();
        this.platformInfoService.deleteInfo(idList);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }
}
